package com.atguigu.gmall.product.api;

import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author 钟涛
 * @version 1.0
 * @description: TODO
 * @date 2021/12/10 11:46
 */
@RestController
@RequestMapping("/api/sku")
public class SkuDetailApiController {
    @Autowired
    private SkuInfoService skuInfoService;
    @Autowired
    private SkuImageService skuImageService;
    @Autowired
    private CategoryViewService categoryViewService;
    @Autowired
    private SpuSaleAttrValueService spuSaleAttrValueService;
    /**
     * 查询商品价格
     *
     * @param skuId
     * @return
     */
    @GetMapping("/detail/price/{skuId}")
    public BigDecimal getSkuPriceBySkuId(@PathVariable("skuId") Long skuId) {
        BigDecimal price = skuInfoService.getSkuPrice(skuId);
        return price;
    }

    /**
     * 查询sku基本信息
     *
     * @param skuId
     * @return
     */
    @GetMapping("/info/{skuId}")
    public SkuInfo getSkuInfo(@PathVariable("skuId") Long skuId) {
        SkuInfo skuInfo = skuInfoService.getById(skuId);
        return skuInfo;
    }

    /**
     * 查询sku的所有图片信息
     *
     * @param skuId
     * @return
     */
    @GetMapping("/image/{skuId}")
    public List<SkuImage> getSkuImages(@PathVariable("skuId") Long skuId) {
        QueryWrapper<SkuImage> wrapper = new QueryWrapper<>();
        wrapper.eq("sku_id", skuId);
        List<SkuImage> list = skuImageService.list(wrapper);
        return list;
    }

    /**
     * 查看当前sku的三级分类信息
     */
    @GetMapping("/categoryView/{c3Id}")
    public BaseCategoryView getCategoryView(@PathVariable("c3Id") Long c3Id) {
        return categoryViewService.getCategoryView(c3Id);
    }

    /**
     * spuSaleAttrList: spu销售属性列表
     * <p>
     * 思路:要给页面展示出,当前sku对应的spu的所有销售属性名和值
     */
    @GetMapping("/saleattrs/{skuId}/{spuId}")
    public List<SpuSaleAttr>  getSpuSaleAttrBySkuIdAndSpuId(@PathVariable("skuId") Long skuId, @PathVariable("spuId") Long spuId) {
        List<SpuSaleAttr> list = spuSaleAttrValueService.getSpuSaleAttrBySkuIdAndSpuId(skuId,spuId);
        return list;
    }

    /**
     * valueSkuJson: 保存一对销售属性组合对应是那个商品
     */
    @GetMapping("/valuesSkuJson/{spuId}")
     public Map<String,String> getValuesSkuJson(@PathVariable("spuId") Long spuId){
         return spuSaleAttrValueService.getValuesSkuJson(spuId);
     }
}
